package com.idp.web.controller;

import com.idp.common.base.BaseController;
import com.idp.common.base.BaseResult;
import com.idp.common.constants.CommonConstants;
import com.idp.common.model.UploadModel;
import com.idp.common.persistence.Page;
import com.idp.common.util.UploadUtils;
import com.idp.common.util.ValidateUtils;
import com.idp.web.dto.NurseListDTO;
import com.idp.web.dto.NurseShowDTO;
import com.idp.web.entity.Nurse;
import com.idp.web.service.INurseService;
import com.idp.web.vo.NurseSearchVo;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 护士controller
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/3/1
 */
@Controller
@RequestMapping("/nurse")
public class NurseController extends BaseController {

    private Logger logger = Logger.getLogger(NurseController.class);

    @Resource
    private INurseService nurseService;

    /**
     * 初始化查询页面--管理后台
     *
     * @return
     */
    @RequestMapping("/init")
    public String init() {
        return "backstage/nurse/nurseSearch";
    }

    /**
     * 初始化查询页面--国内医院
     *
     * @return
     */
    @RequestMapping("/hospitalNurseInit")
    public String hospitalNurseInit() {
        return "hospital/nurse/nurseSearch";
    }

    /**
     * 根据条件分页查询医生列表
     *
     * @param nurseSearchVo 查询信息
     * @param page          分页信息
     * @param request       HttpServletRequest
     * @return String
     */
    @RequestMapping("/list")
    public String list(NurseSearchVo nurseSearchVo, Page<NurseListDTO> page, HttpServletRequest request) {
        try {
            if (getCurrentUser().getUserType() == CommonConstants.USER_TYPE_HOSPITAL){
                nurseSearchVo.setHospitalId(getUserId());
            }
            request.setAttribute("page", nurseService.findByPage(nurseSearchVo, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        if (CommonConstants.NURSE_PAGE_TYPE_HOSPITAL.equals(nurseSearchVo.getPageType())){
            return "hospital/nurse/nurseList";
        }
        return "backstage/nurse/nurseList";
    }

    /**
     * 新增修改页面初始化
     *
     * @param nurseId 护士id
     * @param request HttpServletRequest
     * @return String
     */
    @RequestMapping("/nurse")
    public String nurse(String nurseId, NurseSearchVo nurseSearchVo, HttpServletRequest request) {
        try {
            if (ValidateUtils.isNotEmpty(nurseId)) {
                Nurse nurse = nurseService.getById(nurseId);
                request.setAttribute("nurse", nurse);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        if (CommonConstants.NURSE_PAGE_TYPE_HOSPITAL.equals(nurseSearchVo.getPageType())){
            return "hospital/nurse/nurse";
        }
        return "backstage/nurse/nurse";
    }

    /**
     * 保存或修改
     *
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public String save(HttpServletRequest request) {
        JSONObject json = new JSONObject();
        try {
            Nurse nurse = new Nurse();
            UploadModel model = new UploadModel(UploadUtils.getServerUploadBasePath(UploadUtils.PATH_IMAGES),
                    UploadModel.IMAGES, nurse);
            UploadUtils.uploadFileSealedObject(request, model);

            if (ValidateUtils.isNotEmpty(nurse.getId())) {
                // 修改

                String strMiddle = request.getParameter(CommonConstants.AVATAR_URL);
                if (StringUtils.isEmpty(nurse.getAvatarUrl()) && StringUtils.isNotEmpty(strMiddle)){
                    Nurse nurse1 = nurseService.getById(nurse.getId());
                    nurse.setAvatarUrl(nurse1.getAvatarUrl());
                }
                nurseService.update(nurse, getUserId() ,getCurrentUser().getUserType());
            }else {
                // 新增

                BaseResult baseResult = nurseService.add(nurse,getUserType(),getUserId());
            }
            json.put("result", "save_success");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            json.put("result", "save_fail");
        }
        return json.toString();
    }

    /**
     * 查看页面初始化
     *
     * @param id      护士id
     * @param request HttpServletRequest
     * @return String
     */
    @RequestMapping("/show")
    public String show(String id, NurseSearchVo nurseSearchVo, HttpServletRequest request) {
        if (ValidateUtils.isNotEmpty(id)) {
            NurseShowDTO nurse = nurseService.getNurseById(id);
            request.setAttribute("nurse", nurse);
        } else {
            logger.info("参数错误");
        }
        if (CommonConstants.NURSE_PAGE_TYPE_HOSPITAL.equals(nurseSearchVo.getPageType())){
            return "hospital/nurse/nurseShow";
        }
        return "backstage/nurse/nurseShow";
    }

    /**
     * 审核
     *
     * @param nurse 护士信息
     * @return
     */
    @RequestMapping("/audit")
    @ResponseBody
    public BaseResult audit(Nurse nurse) {
        return nurseService.audit(nurse, getUserId());
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String id) {
        JSONObject json = new JSONObject();
        try {
            nurseService.delete(id);
            json.put("result", "delete_success");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            json.put("result", "delete_fail");
        }
        return json.toString();
    }

    /**
     * 查询护士的统计信息
     *
     * @return BaseResult
     */
    @RequestMapping("/queryStatistics")
    @ResponseBody
    public BaseResult queryStatistics() {
        JSONObject json = new JSONObject();
        Integer userType = null;

        NurseSearchVo nurseSearchVo = new NurseSearchVo();
        if (getCurrentUser().getUserType() == CommonConstants.USER_TYPE_HOSPITAL){
            nurseSearchVo.setHospitalId(getUserId());
        }
        //全部
        nurseSearchVo.setType(CommonConstants.INT_ZERO);
        json.put("nurseAll", nurseService.queryStatistics(nurseSearchVo));
        //待审核
        nurseSearchVo.setType(CommonConstants.NURSE_STATUS_WAIT);
        json.put("nurseWait", nurseService.queryStatistics(nurseSearchVo));
        //审核通过
        nurseSearchVo.setType(CommonConstants.NURSE_STATUS_SUCCESS);
        json.put("nurseSuccess", nurseService.queryStatistics(nurseSearchVo));
        //审核失败
        nurseSearchVo.setType(CommonConstants.NURSE_STATUS_FAIL);
        json.put("nurseFail", nurseService.queryStatistics(nurseSearchVo));
        return sendResult200(json);
    }
}
